local tcp = ngx.socket.tcp

-- connect to local http server

local sock, err = tcp()
if not sock then
  ngx.say("tcp() error")
  return
end

ngx.log(ngx.ERR,"=====done tcp()")

local ip = '127.0.0.1'
local port = 32768
local ok, err = sock:connect(ip, port)
if not ok then
  ngx.say("connect() error")
  return
end
ngx.log(ngx.ERR,"=====done connect()")

local req = "GET / HTTP/1.1\r\n"
            .."Connection: close\r\n"
            .."host: localhost\r\n\r\n"

local bytes, err = sock:send(req)
if not bytes then
  ngx.say("send() error")
  return
end
ngx.log(ngx.ERR,"=====done send()")

local read_headers = sock:receiveuntil("\r\n\r\n")
local headers, err = read_headers()
if not read_headers then
	ngx.say("failed to read response headersock:", err)
	return
end
ngx.say(headers)

local body,err = sock:receive("*a")
if not body then
	ngx.say("failed to read response body", err)
	return
end
ngx.say(body)

sock:close()
